package com.wlts.wzy.DYOrderModule.util;

import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.DYGoodsModule.DYapi.DySdk;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYOrderModule.service.IDyOrdersService;
import com.wlts.wzy.DYUserModule.entity.DyOauth;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyOauthService;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.LMGoodsModule.entity.LmSelfGoods;
import com.wlts.wzy.LMGoodsModule.service.ILmSelfGoodsService;
import com.wlts.wzy.api.request.dy.PageOrderToDyApiRequest;
import com.wlts.wzy.api.response.dy.UpdateDyOrdersResponse;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DyOrdersUpdate {

    @Autowired
    private IDyOauthService ksOauthService;

    @Autowired
    private IDyStoreUserService dyStoreUserService;
    @Autowired
    private IDyOrdersService dyOrdersService;
    @Autowired
    private ILmSelfGoodsService lmSelfGoodsService;

    @Scheduled(fixedDelay = 120 * 1000)
    // @PostConstruct
    /**
     * 将抖音订单更新到本地库
     */
    public void updateDyOrders() {


        System.out.println("更新抖音订单 计时器");

        MyLambdaQueryWrapper<DyOauth> ksOauthWrapper = new MyLambdaQueryWrapper<>();

        ksOauthWrapper.eq(DyOauth::getDelFlag, "0");
        ksOauthWrapper.ge(DyOauth::getTokenOverTime, LocalDateTime.now());
        ksOauthWrapper.eq(DyOauth::getTokenStatus, 0);
        List<DyOauth> oauthList = ksOauthService.list(ksOauthWrapper);

        oauthList.forEach(ksOauth -> {

            MyLambdaQueryWrapper<DyStoreUser> wrapper = new MyLambdaQueryWrapper<>();

            wrapper.eq(DyStoreUser::getUuid, ksOauth.getUserUuid());

            wrapper.eq(BaseEntity::getDelFlag, "0");

            //      wrapper.eq(DyStoreUser::getPassword, MD5.md5(request.getPassword()));

            DyStoreUser dyStoreUser = dyStoreUserService.getOne(wrapper);

            PageOrderToDyApiRequest orderListRequest = new PageOrderToDyApiRequest();

            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime time = LocalDateTime.now();

           // orderListRequest.setOrder_status(2 + "");
            orderListRequest.setStart_time(df.format(time.minusDays(1)));
            orderListRequest.setEnd_time(df.format(time));

//            orderListRequest.setStart_time("2021-04-17 00:00:00");
//            orderListRequest.setEnd_time("2021-04-17 12:00:00");

            orderListRequest.setIs_desc(1 + "");
            orderListRequest.setOrder_by("update_time");

            int page = 0;

            while (true) {
                orderListRequest.setPage(page + "");
                orderListRequest.setSize(100 + "");

                List<UpdateDyOrdersResponse> responseList = DySdk.orderList(dyStoreUser.getAppKey(), dyStoreUser.getAppSecret(), ksOauth.getAccessToken(), orderListRequest);

                List<DyOrders> ksOrdersList = responseList.stream().map(a -> {
                    MyLambdaQueryWrapper<DyOrders> orderWrapper = new MyLambdaQueryWrapper<>();
                    orderWrapper.eq(DyOrders::getOrderId, a.getOrderId());
                    orderWrapper.eq(DyOrders::getDelFlag, "0");
                    DyOrders orders = dyOrdersService.getOne(orderWrapper);

                    if (orders == null) {
                        orders = new DyOrders();
                    }else {
                        a.setOutProductId(null);
                        a.setOutSkuId(null);
                    }


                    CopyUtils.copyProperties(a, orders);


                    dyOrdersService.saveOrUpdate(orders);

                    return orders;
                }).collect(Collectors.toList());

//                if (ksOrdersList.size() > 0) {
//                    dyOrdersService.saveOrUpdateBatch(ksOrdersList);
//                }
                page += 1;

                if (responseList.size() < 100) {
                    break;
                }

            }
        });

    }


    /**
     * 判断是否平台订单
     */
    @Scheduled(fixedDelay = 222 * 1000)
    public void checkPTOrders() {

        System.out.println("判断是否平台订单 计时器");

        MyLambdaQueryWrapper<DyOrders> queryWrapper = new MyLambdaQueryWrapper<>();

        queryWrapper.in(DyOrders::getSelfOrder, 0);//平台商品的订单 0未确定 1是 2不是

        List<DyOrders> ksOrdersList = dyOrdersService.list(queryWrapper);

        ksOrdersList.forEach(ksOrders -> {

            MyLambdaQueryWrapper<LmSelfGoods> selfGoodsWrapper = new MyLambdaQueryWrapper<>();

            selfGoodsWrapper.eq(LmSelfGoods::getRelItemId, ksOrders.getOutProductId());

            LmSelfGoods lmSelfGoods = lmSelfGoodsService.getOne(selfGoodsWrapper);

            if (lmSelfGoods == null) {
                ksOrders.setSelfOrder(2);//如果商品不存在，证明不是平台推送的商品
                dyOrdersService.updateById(ksOrders);
            } else {
                ksOrders.setSelfOrder(1);
                dyOrdersService.updateById(ksOrders);
            }
        });
    }
}
